define(["jquery","cookie"],function($){
    function Header(){
        this.load();
    }

    Header.prototype = {
        constructor: Header,

        load:function(){
            $.get("/html/include/header.html",$.proxy(this.headerHandler,this));
            $.get("/html/include/fixed.html",$.proxy(this.fixedHandler,this));
            $.get("/html/include/top.html",$.proxy(this.topHandler,this));
            $.get("/html/include/nav.html",$.proxy(this.navHandler,this));
            $("footer").load("/html/include/footer.html");
        },

        headerHandler:function(data){
            $("header").html(data);
            this.addListener();
        },

        fixedHandler: function(data){
            $(".fixed").html(data);
            this.addListener();
            this.loadNum();
        },
        topHandler: function(data){
            $(".top1").html(data);
            this.loadLoginUser();
        },

        navHandler: function(data){
            $("nav").html(data);
            this.addListener();
            
        },

        addListener: function(){
            // console.log($(".search-alert"));
            $(".search-box>input").keyup(this.searchHandler);
            $(".search-alert").on("click","div",this.suggestHandler);
            // $(".search-box>input").blur(this.emtpySuggest);
            $(".cate_posi").hover(function(){
                // console.log(1);
                $(".main_cate").stop().slideToggle(200);
            },function(){
                // console.log(1);
                $(".main_cate").stop().slideToggle(200);
            });

            $(".cart").on("click",function(){
                location = "/html/cart.html";
            });

            $(".logout").on("click",this.logoutHandler);
            $(".qiaquan-btn,.my-btn,.backHome,.backTop").hover(this.fadeInAnimate,this.fadeOutAnimate);
            $(".backTop").on("click",this.backTop);
        },

        searchHandler: function(){
            
            var word = $(this).val(),
                url = `https://suggest.taobao.com/sug?code=utf-8&q=${word}&callback=?`;
            $.getJSON(url,function(data){
                var html = "";
                if(data.result.length > 0){
                    $(".search-alert").show();
                }
                data.result.forEach(function(curr){
					html += `<div class="td">${curr[0]}</div>`;
                });
            $(".search-alert").html(html);
            });
        },

        suggestHandler: function(){
            // console.log(1);
            $(".search-box>input").val($(this).text());
            $(".search-alert").empty();
            $(".search-alert").hide();
        },

        emtpySuggest: function(){
            $(".search-alert").empty();
            $(".search-alert").hide();
        },

        loadLoginUser:function(){
            $.cookie.json = true;
            var user = $.cookie("login-user");
            // console.log(user);
            if(user){
                $(".link-login").parent("li").html(`<a>${user}</a><a href="javascript:void(0)" class="logout">[退出]</a><span>|</span>`)
            }
        },

        loadNum :function(){
            $.cookie.json = true;

            var products = $.cookie("cart");

            // console.log(products);
            var num = 0;
            products.forEach(function(prod){
                num += prod.amount;
            });

            $(".amountall").text(num);
        },

        logoutHandler: function(){
            console.log(1);
            $.cookie.json = true;
            $.removeCookie("login-user");
            $(".username").html(`<a href="/html/login.html" class="link-login">登陆</a><span>|</span>`);
        },

        fadeInAnimate: function(){
            var src = $(this).siblings("p");
            src.stop().show().animate({left:"-68px",opacity:"1"},300);

        },
        fadeOutAnimate: function(){
            var src = $(this).siblings("p");
            src.stop().animate({left:"-90px",opacity:"0"},300,function(){
                src.hide(); 
            });
        },

        backTop:function(){
            $("html,body").animate({scrollTop:0},500);
            
        }
    }

    new Header();
});